package com.jhgsys.internal.sypq.service.impl;

import com.jhgsys.internal.sypq.entity.SypqProject;
import com.jhgsys.internal.sypq.entity.WorkflowRecord;
import com.jhgsys.internal.sypq.mapper.WorkflowRecordMapper;
import com.jhgsys.internal.sypq.service.ISypqProjectService;
import com.jhgsys.internal.sypq.service.IWorkflowRecordService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.*;

/**
 * 项目审批 Service实现
 *
 * @author ganjun
 * @date 2024-11-16 21:20:36
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkflowRecordServiceImpl extends ServiceImpl<WorkflowRecordMapper, WorkflowRecord> implements IWorkflowRecordService {

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-16 21:20:36
     * @param request QueryRequest
     * @param workflowRecord workflowRecord
     * @return IPage<WorkflowRecord>
    */
    @Override
    public IPage<WorkflowRecord> findWorkflowRecordPage(QueryRequest request, WorkflowRecord workflowRecord) {
        LambdaQueryWrapper<WorkflowRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowRecord::getDeletedFlg, 0);
        queryWrapper.eq(WorkflowRecord::getVoucherId,workflowRecord.getVoucherId());
        queryWrapper.eq(WorkflowRecord::getVoucherType,workflowRecord.getVoucherType() );
        // TODO 设置查询条件

        Page<WorkflowRecord> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-16 21:20:36
     * @param workflowRecord workflowRecord
     * @return List<WorkflowRecord>
     */
    @Override
    public List<WorkflowRecord> findWorkflowRecordList(WorkflowRecord workflowRecord) {
	    LambdaQueryWrapper<WorkflowRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowRecord::getDeletedFlg, 0);
        if(workflowRecord.getVoucherType()!=null){
            queryWrapper.eq(WorkflowRecord::getVoucherType,workflowRecord.getVoucherType());
        }
        if(workflowRecord.getVoucherId()!=null){
            queryWrapper.eq(WorkflowRecord::getVoucherId,workflowRecord.getVoucherId());
        }

        if(workflowRecord.getStatus()!=null){
            queryWrapper.eq(WorkflowRecord::getStatus,workflowRecord.getStatus());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author ganjun
    * @date 2024-11-16 21:20:36
    * @param id
    * @return WorkflowRecord
    */
    @Override
    public WorkflowRecord findWorkflowRecordById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author ganjun
     * @date 2024-11-16 21:20:36
     * @param workflowRecord workflowRecord
     */
    @Override
    @Transactional
    public boolean createWorkflowRecord(WorkflowRecord workflowRecord) {
        return this.save(workflowRecord);
    }


    /**
     * 修改
     * @author ganjun
     * @date 2024-11-16 21:20:36
     * @param workflowRecord workflowRecord
     */
    @Override
    @Transactional
    public boolean updateWorkflowRecord(WorkflowRecord workflowRecord) {
        return this.updateById(workflowRecord);
    }

    /**
     * 逻辑删除
     * @author ganjun
     * @date 2024-11-16 21:20:36
     * @param workflowRecord workflowRecord
     * @param workflowRecordIds workflowRecordIds
     */
    @Override
    @Transactional
    public boolean deleteWorkflowRecord(WorkflowRecord workflowRecord,String workflowRecordIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(workflowRecordIds.split(StringPool.COMMA));
        workflowRecord.setDeletedBy(user.getUserName());
        workflowRecord.setDeletedTime(new Date());
        workflowRecord.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(workflowRecord,new LambdaQueryWrapper<WorkflowRecord>().in(WorkflowRecord::getId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author ganjun
    * @date 2024-11-16 21:20:36
    * @param workflowRecordIds workflowRecordIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteWorkflowRecord(String workflowRecordIds) {
        List<String> list = Arrays.asList(workflowRecordIds.split(StringPool.COMMA));
        LambdaQueryWrapper<WorkflowRecord> wapper = new LambdaQueryWrapper<>();
        wapper.in(WorkflowRecord::getId,list);
        return this.remove(wapper);
    }
}
